export default class Module {
  
  #children = Object.create(null)
  #rawModule = Object.create(null)
  state
  get namespaced() {
    return !!this.#rawModule.namespaced
  }

  constructor (rawModule) {
    this.#rawModule = rawModule
    this.state = rawModule.state
  }

  addChild (name, module) {
    this.#children[name] = module
  }

  getChild (name) {
    return this.#children[name]
  }

  forEachChild (cb) {
    for (const [name, child] of Object.entries(this.#children)) {
      cb(name, child)
    }
  }

  forEachGetter (cb) {
    if (this.#rawModule.getters) {
      for (const [name, handler] of Object.entries(this.#rawModule.getters)) {
        cb(name, handler)
      }
    }
  }

  forEachAction (cb) {
    if (this.#rawModule.actions) {
      for (const [name, handler] of Object.entries(this.#rawModule.actions)) {
        cb(name, handler)
      }
    }
  }

  forEachMutation (cb) {
    if (this.#rawModule.mutations) {
      for (const [name, handler] of Object.entries(this.#rawModule.mutations)) {
        cb(name, handler)
      }
    }
  }

}
